package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.entity.ExtensionTrainPersonnel;
import com.zito.health.service.AreaService;
import com.zito.health.service.ExtensionTrainPersonnelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Sam
 * @time: 2020/03/14 17:33
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/extensionTrainPersonnel")
public class ExtensionTrainPersonnelController {


    @Autowired
    private ExtensionTrainPersonnelService extensionTrainPersonnelService;

    @Autowired
    private AreaService areaService;

    @GetMapping
    public PageResult list(ExtensionTrainPersonnel param, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<ExtensionTrainPersonnel> result = new PageResult();
        if (pageNo != null && pageSize != null) {
            PageHelper.startPage(pageNo, pageSize);
        }
        List<ExtensionTrainPersonnel> list = extensionTrainPersonnelService.query(param);
        PageInfo<ExtensionTrainPersonnel> pageInfo = new PageInfo<ExtensionTrainPersonnel>(list);
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return result;
    }


    /**
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ExtensionTrainPersonnel queryById(@PathVariable Integer id) {
        ExtensionTrainPersonnel extensionTrainPersonnel = extensionTrainPersonnelService.queryById(id);
        return extensionTrainPersonnel;
    }

//    @PostMapping
//    public VerifyResult save(ExtensionTrainPersonnel extensionTrainPersonnel) {
//        VerifyResult verifyResult = new VerifyResult();
//        extensionTrainPersonnelService.save(extensionTrainPersonnel);
//        return verifyResult;
//    }

    @PutMapping
    public VerifyResult update(ExtensionTrainPersonnel extensionTrainPersonnel) {
        VerifyResult verifyResult = new VerifyResult();
        extensionTrainPersonnelService.update(extensionTrainPersonnel);
        return verifyResult;
    }


    @DeleteMapping("/{id}")
    public VerifyResult delete(@PathVariable Integer id) {
        VerifyResult verifyResult = new VerifyResult();
        extensionTrainPersonnelService.delete(id);
        return verifyResult;
    }

    /**
     * 培训人员导入
     *
     * @param file 需要导入的文件
     * @param id
     * @return
     */
    @PostMapping("/importSave")
    public VerifyResult user_importSave(HttpServletRequest request, @RequestParam(value = "wenjian", required = false) MultipartFile file, String id) {
        Integer id1 = Integer.parseInt(id);

        String filename = file.getOriginalFilename();
        String houzhui = filename.substring(filename.lastIndexOf("."));

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        Map<String, Object> map1 = null;

        if (".xlsx".equals(houzhui.trim()) || ".xls".equals(houzhui.trim())) {

            try {
                InputStream input = file.getInputStream();
                FileInputStream fis = (FileInputStream) (input);
                HSSFWorkbook wb = new HSSFWorkbook(fis);

                // 获取第一个sheet
                HSSFSheet sheet = wb.getSheetAt(0);
                // 总行数 获取一共有多少行
                int rowNum = sheet.getLastRowNum();
                System.out.println("一共有：" + rowNum);
                // 从第二行开始 第一行一般为标题
                HSSFRow row = sheet.getRow(1);
                // 列数
                int lonNum = 0;

                // 导入想对应的项目id
                for (int i = 1; i <= rowNum; i++) {
                    map = new HashMap<String, Object>();
                    map.put("trainId", id1);
                    list.add(map);
                }

                // 导入表里的姓名列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(0);
                    String name = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);
                        return js;
                    } else {
                        name = cell.getStringCellValue();
                    }
                    map = new HashMap<String, Object>();
                    map.put("name", name);
                    list.add(map);
                }
                Integer education1;
                // 导入表里的学历列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(1);
                    String education = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        //js.setSuccess(false);
                        js.setResult(false);

                        return js;
                    } else {

                        education = cell.getStringCellValue();

                    }
                    if (education == "高中" || "高中".equals(education)) {
                        education1 = 1;
                    } else if (education == "大专" || "大专".equals(education)) {
                        education1 = 2;
                    } else if (education == "本科" || "本科".equals(education)) {
                        education1 = 3;
                    } else if (education == "硕士" || "硕士".equals(education)) {
                        education1 = 4;
                    } else if (education == "博士" || "博士".equals(education)) {
                        education1 = 5;
                    } else {
                        education1 = 0;
                    }
                    map = new HashMap<String, Object>();
                    map.put("education", education1);
                    list.add(map);
                }

                // 导入表里的区域列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(2);
                    String divisionId = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        //js.setSuccess(false);
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        divisionId = cell.getStringCellValue();
                    }
                    Map<String, Object> param123 = new HashMap<String, Object>();
                    param123.put("areaName", divisionId);
                    Integer divisionId1 = 0;
                    if (areaService.selectByParam(param123).size() != 0) {
                        divisionId1 = areaService.selectByParam(param123).get(0).getAreaId();
                    }
                    map = new HashMap<String, Object>();
                    map.put("areaId", divisionId1);
                    list.add(map);
                }

                // 导入表里的年龄列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(3);
                    String ages = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        //js.setSuccess(false);
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        ages = cell.getStringCellValue();

                    }
                    Integer ages1 = Integer.parseInt(ages);
                    map = new HashMap<String, Object>();
                    map.put("ages", ages1);
                    list.add(map);
                }

                // 导入表里的身份证号列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(4);
                    String sid = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        sid = cell.getStringCellValue();

                    }
                    map = new HashMap<String, Object>();
                    map1 = new HashMap<String, Object>();
                    map.put("sid", sid);
                    map1.put("sid", sid);
                    map1.put("trainId", id1);
                    list.add(map);
                    int a = extensionTrainPersonnelService.selectSidCount(map1);
                    if (a > 0) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("身份证号码重复!请核对人员信息是否有重复!");
                        js.setResult(false);

                        return js;
                    }
                }

                // 导入表里的电话列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(5);
                    String phone = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        phone = cell.getStringCellValue();

                    }
                    /* Integer phone1 =Integer.parseInt(phone); */
                    map = new HashMap<String, Object>();
                    map.put("phone", phone);
                    list.add(map);
                }

                // 导入表里的培训得分列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(6);
                    String scoreTrain = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        scoreTrain = cell.getStringCellValue();

                    }
                    float scoreTrain1 = Float.parseFloat(scoreTrain);
                    map = new HashMap<String, Object>();
                    map.put("scoreTrain", scoreTrain1);
                    list.add(map);
                }

                // 导入表里的负责人对培训人员打分列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(7);
                    String scoreIncharge = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        scoreIncharge = cell.getStringCellValue();

                    }
                    float scoreIncharge1 = Float.parseFloat(scoreIncharge);

                    map = new HashMap<String, Object>();
                    map.put("scoreIncharge", scoreIncharge1);
                    list.add(map);
                }

                // 导入表里的培训人员对负责人打分打分列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(8);
                    String scorePersonnel = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        scorePersonnel = cell.getStringCellValue();

                    }
                    float scorePersonnel1 = Float.parseFloat(scorePersonnel);
                    map = new HashMap<String, Object>();
                    map.put("scorePersonnel", scorePersonnel1);
                    list.add(map);
                }

                // 导入表里的单位列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(9);
                    String department = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        department = cell.getStringCellValue();

                    }
                    map = new HashMap<String, Object>();
                    map.put("department", department);
                    list.add(map);
                }
                // 导入表里的职称列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(10);
                    String title = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        title = cell.getStringCellValue();

                    }
                    map = new HashMap<String, Object>();
                    map.put("title", title);
                    list.add(map);
                }
                // 导入表里的专业列取值
                for (int i = 1; i <= rowNum; i++) {
                    row = sheet.getRow(i);
                    // 获取该行有多少列
                    //lonNum = row.getLastCellNum();
                    //log.info("获取到了"+lonNum+"行");
                    HSSFCell cell = row.getCell(11);
                    String major = null;
                    if (cell == null) {
                        VerifyResult js = new VerifyResult();
                        js.setMsg("请仔细检查文件内容，查看是否有空缺或者重名");
                        js.setResult(false);

                        return js;
                    } else {
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        major = cell.getStringCellValue();

                    }
                    map = new HashMap<String, Object>();
                    map.put("major", major);
                    list.add(map);
                }
                extensionTrainPersonnelService.insertImport(list);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            VerifyResult j = new VerifyResult();
            j.setMsg("ok");
            j.setResult(true);
            return j;
        } else {
            VerifyResult j = new VerifyResult();
            j.setMsg("上传文件类型不匹配");
            j.setResult(false);

            return j;
        }

    }

    /**
     * 批量删除培训人员
     *
     * @param ids 培训人员所有id,中间以逗号分割开
     * @return
     */
    @DeleteMapping("/batchDelete")
    public VerifyResult batchDeletionOfTrainers(String ids) {
        VerifyResult verifyResult = new VerifyResult();
        int i = extensionTrainPersonnelService.deleteByIds(ids);
        if (i > 0) {
            return verifyResult;
        } else {
            verifyResult.setResult(false);
            verifyResult.setMsg("系统错误");
            return verifyResult;
        }

    }

}
